﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using Beeldbankmvc.Models;
using Beeldbankmvc.ViewModels;

namespace Beeldbankmvc.Controllers
{
    [HandleErrorWithELMAH]
    public class ShoppingCartController : Controller
    {
        BeeldbankLinqDataContext beeldBankDB = new BeeldbankLinqDataContext();
        UserRepository userRespository = new UserRepository();
        //
        // GET: /ShoppingCart/

        public ActionResult Index()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);
            ShoppingCartViewModel viewModel = null;
  

            MembershipUser currentUser = Membership.GetUser();
            if (currentUser != null)
            {
                // Set up our ViewModel
                Guid userGuid = new Guid(currentUser.ProviderUserKey.ToString());   
                viewModel = new ShoppingCartViewModel
                {
                    CartItems = cart.GetCartItems(),

                    CartTotal = cart.GetTotal(),
                    CartUserId = cart.GetCartId(this.HttpContext),
                    currentAccountModel = userRespository.FindOne(userGuid)
                };
            }else{
                // Set up our ViewModel
                viewModel = new ShoppingCartViewModel
                {
                    CartItems = cart.GetCartItems(),
                    CartUserId = cart.GetCartId(this.HttpContext),
                    CartTotal = cart.GetTotal()
                };
            }
            // Return the view
            return View(viewModel);
        }

        //
        // GET: /Store/AddToCart/5

        public ActionResult AddToCart(int id, int amount, string type, string size, string finishing, int mediaTypeId)
        {

            // Retrieve the media object from the database
            var addedMediaObject = beeldBankDB.BEELDBANK_MEDIAOBJECTs
                .Single(MediaObject => MediaObject.Id == id);

            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            cart.AddToCart(addedMediaObject, amount, type, size, finishing, mediaTypeId );

            // Go back to the main store page for more shopping
            return RedirectToAction("Index");
        }

        //
        // GET: /ShoppingCart/RemoveFromCart/5
        public ActionResult RemoveFromCart(int id)
        {
            // Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            // Remove from cart. Note that for simplicity, we're 
            // removing all rather than decrementing the count.
            cart.RemoveFromCart(id);

            // Go back to the main store page for more shopping
            return RedirectToAction("Index");
        }

        //
        // GET: /ShoppingCart/CartSummary

        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            ViewData["CartCount"] = cart.GetCount();

            return PartialView("CartSummary");
        }

        //
        // GET: /ShoppingCart/CheckOut
        public ActionResult CheckOut()
        {
            // A user that previously wasn't logged in, tried to access the checkout
            // The login page redirects to the GET CheckOut, not the POST CheckOut
            return RedirectToAction("Index");
        }
        
        //
        // POST: /ShoppingCart/CheckOut
        [HttpPost]
        [Authorize]
        public ActionResult CheckOut(int i)
        {
            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            cart.SaveShoppingCartToOrder(Request);
            return View();
        }

        [Authorize]
        public ActionResult ThankYou()
        {
            return View();
        }
    }
}